package com.hdoit.kylin.logical.proposal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.kylin.logical.proposal.dao.IProposalDao;
import com.hdoit.kylin.logical.proposal.model.Proposal;
import com.hdoit.kylin.logical.user.model.Student;

/**
 * @author tanzulie
 * @email
 * @createtime 2017年08月01日 14:52:41
 * @comment
 */
@Component
public class ProposalDaoImpl extends SuperDao<Proposal> implements IProposalDao {

	@Override
	public List<Proposal> queryListByLoginId(String loginId, String groupId,
			String role) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		if ("student".equals(role)) {
			finderExpression.select("proposal.* ","student.employeeName as createUserName")
					.from(Proposal.class,"proposal")
					.leftJoin(Student.class,"student")
					.on("proposal.createUserId", "student.employeeId")
					.andWhere("proposal.createUserId=#{loginId}")
					.setParam("loginId", loginId)
					.andWhere("mainMark = 1");
			return queryList(finderExpression);
		} else if ("teacher".equals(role)) {
			finderExpression.select("proposal.* ","student.employeeName as createUserName")
					.from(Proposal.class,"proposal")
					.leftJoin(Student.class,"student")
					.on("proposal.createUserId", "student.employeeId")
					.andWhere("proposal.groupId=#{groupId}")
					.setParam("groupId", groupId)
					.andWhere("mainMark = 1");
			
			return queryList(finderExpression);
		} else if (StringUtils.isEmpty(role)) {
			finderExpression.select("proposal.* ","student.employeeName as createUserName")
					.from(Proposal.class,"proposal")
					.leftJoin(Student.class,"student")
					.on("proposal.createUserId", "student.employeeId")
					.andWhere("mainMark = 1");
			return queryList(finderExpression);
		}
		return null;
	}

	@Override
	public int queryCount(SearchModel search, String loginId, String role)
			throws DaoBaseException {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		if ("student".equals(role)) {
			finderExpression.addBody("select count(1) from " + getTableName())
					.andWhere("proposal.createUserId=#{loginId}")
					.setParam("studentId", loginId)
					.andWhere("mainMark = 1");

			return queryCount(finderExpression, search);
		}
		finderExpression.addBody("select count(1) from " + getTableName());
		return queryCount(finderExpression, search);
	}

	@Override
	public int queryCount(String proposalName, String loginId, String role) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		if ("student".equals(role)) {
			finderExpression.addBody("select count(1) from " + getTableName())
					.andWhere("proposal.createUserId=#{studentId}")
					.andWhere("proposal.proposalName=#{proposalName}")
					.setParam("proposalName", proposalName)
					.setParam("studentId", loginId);

			return queryCount(finderExpression);
		}
		finderExpression.addBody("select count(1) from " + getTableName());
		return queryCount(finderExpression);
	}

	@Override
	public List<Proposal> queryListByProposalName(String proposalName) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();

		finderExpression.addBody("select * from ", Proposal.class)
				.andWhere("proposalName = #{proposalName}")
				.setParam("proposalName", proposalName);
		
		return queryList(finderExpression);

	}
}